package com.example.teachingevaluationsystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teachingevaluationsystem.entity.User;
import com.example.teachingevaluationsystem.service.EvaluateService;
import com.example.teachingevaluationsystem.service.IUserService;
import com.example.teachingevaluationsystem.utils.ExcelExportUtils;
import com.example.teachingevaluationsystem.vo.ScoreVo;
import com.example.teachingevaluationsystem.vo.ScoringData;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

/**
 * 评价 前端控制器
 *
 * @author lp
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/evaluate/evaluate")
@AllArgsConstructor
public class EvaluateController {

    private final EvaluateService evaluateService;

    private final IUserService userService;

    private final ExcelExportUtils excelExportUtil;

    @GetMapping("/result")
    public List<ScoreVo> getScoreVo(
            Principal principal,
            @RequestParam(required = false) Long planId,
            @RequestParam(required = false) Long courseId,
            @RequestParam(required = false) Boolean isTeacher) {
        final User user = userService.loadUserByUserName(principal.getName());
        return evaluateService.getScoreVo(user.getId(), planId, courseId, isTeacher);
    }

    @GetMapping("/excel")
    public void exportExcel(
            Principal principal,
            @RequestParam(required = false) Long planId,
            @RequestParam(required = false) Long courseId)
            throws IOException {
        final User user = userService.loadUserByUserName(principal.getName());
        List<ScoringData> scoringData = evaluateService.export(user.getId(), planId, courseId);
        excelExportUtil.exportExcel(
                scoringData, ScoringData.class, "TEST", "sheetName", "fileName");
    }
}
